
#include "Config.h"
#include "reg.h"
#include "typedefs.h"
#include "Printf.h"
#include "I2C.h"
#include "CPU.h"
#include "SPI.h"
#include "NVMData.h"
#include "Global.h"
#include "TouchScreen.h"
#include "OSD\dv_OSD.h"
#include "AdjTSC.h"

#define OSDPT_LU_X 80
#define OSDPT_LU_Y 48
#define OSDPT_LD_X 80
#define OSDPT_LD_Y 432
#define OSDPT_RD_X 720
#define OSDPT_RD_Y 432
#define OSDPT_RU_X 720
#define OSDPT_RU_Y 48
#define OSDPT_CC_X 400
#define OSDPT_CC_Y 240

#define CHK_PT_DIFF 10

enum
{
    ADJ_PT_LU=0x0,
    ADJ_PT_LD=0x10,
    ADJ_PT_RD=0x20,
    ADJ_PT_RU=0x30,
    ADJ_PT_CC=0x40
};


S8 Xtmpslope=0, Ytmpslope=0;
LONG Xtmpoff=0, Ytmpoff=0;
U16 Tmpadc, Xadc, Yadc;
BYTE ADJState;

void DrwOSDPt(U16 osdptx, U16 osdpty);
void ClrOSDPt(U16 osdptx, U16 osdpty);
void AdjStage0(void);
BYTE AdjStage1(void);
void AdjStage2(void);
void AdjTSC(void);

void AdjStage0(void)
{
	//OSD_ShowWindowBmp(0, 0);				// disable 8bit
	//OSD_ShowWindowBmp(1, 0);				// disable 8bit


	//TWLL_OSDLayer_Init();	
	//TWLL_Canvas_Init(&User_Canvas[0]);
	OSD_BlockFill(0, 480, 800, 480, 0x0018, 16);
	DrwOSDPt(OSDPT_LU_X, OSDPT_LU_Y);
	ADJState = ADJ_PT_LU;
	Xres = 4095;
	Yres = 4095;
	Xslope = 10;
	Yslope = 10;
	Xoff = 0;
	Yoff = 0;
	
	return;
}

BYTE AdjStage1(void)
{
	BYTE State;
	TSPOINT adctmppt;
	S8 tmpslope;
	LONG tmpoff;
	
	State = TOUCH_IDLE;
	
	IntTouch();
	GetTouchInfo(&adctmppt, &State);
	
	State |= ADJState;
	switch (State){
    case ADJ_PT_LU|TOUCH_IDLE:
    case ADJ_PT_LD|TOUCH_IDLE:
    case ADJ_PT_RD|TOUCH_IDLE:
    case ADJ_PT_RU|TOUCH_IDLE:
    case ADJ_PT_CC|TOUCH_IDLE:
		break;
    case ADJ_PT_LU|TOUCH_PRESS:
	case ADJ_PT_LU|TOUCH_HOLD:
		Yadc = adctmppt.y;
		Tmpadc = adctmppt.x;
		break;
    case ADJ_PT_LD|TOUCH_PRESS:
    case ADJ_PT_LD|TOUCH_HOLD:
		CalSlopeOff(Yadc, adctmppt.y, OSDPT_LU_Y, OSDPT_LD_Y, &tmpslope, &tmpoff);
		Ytmpslope = tmpslope;
		Ytmpoff = tmpoff;
		Xadc = adctmppt.x;
		break;
    case ADJ_PT_RD|TOUCH_PRESS:
    case ADJ_PT_RD|TOUCH_HOLD:
		CalSlopeOff(Xadc, adctmppt.x, OSDPT_LD_X, OSDPT_RD_X, &tmpslope, &tmpoff);
		Xtmpslope = tmpslope;
		Xtmpoff = tmpoff;
		Yadc = adctmppt.y;
		break;
    case ADJ_PT_RU|TOUCH_PRESS:
    case ADJ_PT_RU|TOUCH_HOLD:
		CalSlopeOff(Yadc, adctmppt.y, OSDPT_RD_Y, OSDPT_RU_Y, &tmpslope, &tmpoff);
		Ytmpslope = (S8)(((S16)tmpslope + (S16)Ytmpslope)/2);
		Ytmpoff = (tmpoff + Ytmpoff)/2;
		
		CalSlopeOff(adctmppt.x, Tmpadc, OSDPT_RU_X, OSDPT_LU_X, &tmpslope, &tmpoff);
		Xtmpslope = (S8)(((S16)tmpslope + (S16)Xtmpslope)/2);
		Xtmpoff = (tmpoff + Xtmpoff)/2;
		break;
    case ADJ_PT_CC|TOUCH_PRESS:
    case ADJ_PT_CC|TOUCH_HOLD:
		Xadc = adctmppt.x;
		Yadc = adctmppt.y;
		break;
    case ADJ_PT_LU|TOUCH_POPUP:
		ADJState = ADJ_PT_LD;
		ClrOSDPt(OSDPT_LU_X, OSDPT_LU_Y);
		DrwOSDPt(OSDPT_LD_X, OSDPT_LD_Y);
		break;
    case ADJ_PT_LD|TOUCH_POPUP:
		ADJState = ADJ_PT_RD;
		ClrOSDPt(OSDPT_LD_X, OSDPT_LD_Y);
		DrwOSDPt(OSDPT_RD_X, OSDPT_RD_Y);
		break;
    case ADJ_PT_RD|TOUCH_POPUP:
		ADJState = ADJ_PT_RU;
		ClrOSDPt(OSDPT_RD_X, OSDPT_RD_Y);
		DrwOSDPt(OSDPT_RU_X, OSDPT_RU_Y);
		break;
    case ADJ_PT_RU|TOUCH_POPUP:
		ADJState = ADJ_PT_CC;
		ClrOSDPt(OSDPT_RU_X, OSDPT_RU_Y);
		DrwOSDPt(OSDPT_CC_X, OSDPT_CC_Y);
		break;
    case ADJ_PT_CC|TOUCH_POPUP:
		ADJState = ADJ_PT_LU;
		ClrOSDPt(OSDPT_CC_X, OSDPT_CC_Y);
		return 1;
	default:
		break;
	}

	return 0;
}

void AdjStage2(void)
{
	Xres = DEF_XRES;
	Yres = DEF_YRES;
	Xslope = Xtmpslope;
	Yslope = Ytmpslope;
	Xoff = Xtmpoff;
	Yoff = Ytmpoff;

	Tmpadc = ADC2SCR(Xadc, Xslope, Xoff);
	if((Tmpadc<(OSDPT_CC_X-CHK_PT_DIFF))||(Tmpadc>(OSDPT_CC_X+CHK_PT_DIFF))){
		Xslope = DEF_XSLOPE;
		Xoff = DEF_XOFF;
	}
	
	Tmpadc = ADC2SCR(Yadc, Yslope, Yoff);
	if((Tmpadc<(OSDPT_CC_Y-CHK_PT_DIFF))||(Tmpadc>(OSDPT_CC_Y+CHK_PT_DIFF))){
		Yslope = DEF_YSLOPE;
		Yoff = DEF_YOFF;
	}
		
	SaveParaTSC();
	
	return;
}

void AdjTSC(void)
{
	AdjStage0();
	
	do{
	}while((AdjStage1()) == 0);

	AdjStage2();

	return;
}

void DrwOSDPt(U16 osdptx, U16 osdpty)
{
	OSD_BlockFill(osdptx-5, osdpty-5+480, 10, 10, 0x07E0, 16);
	OSD_BlockFill(osdptx-2, osdpty-2+480, 4, 4, 0xF800, 16);
}

void ClrOSDPt(U16 osdptx, U16 osdpty)
{
	OSD_BlockFill(osdptx-5, osdpty-5+480, 10, 10, 0x0018, 16);
}


